<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission('0',1)">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <!-- 添加按钮只在访问权的层级显示 row.type === 1时才显示 -->
            <el-button v-if="row.type === 1" type="text" size="small" @click="addPermission(row.id,2)">添加</el-button>
            <el-button type="text" size="small" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增弹层-->
    <el-dialog :visible="showDialog" :title="title" @close="btnCancel">
      <el-form ref="perForm" :rules="rules" :model="formData" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <!-- 当值为1时激活 当值为0时不激活 -->
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <!-- 底部的确定和取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { updatePermission, addPermission, getPermissionDetail, delPermission, getPermissionList } from '@/api/permisson'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      showDialog: false
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // 将数组转化成带children的树形结构
      this.list = transListToTreeData(await getPermissionList(), '0')
    },
    // 添加
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 删除
    async delPermission(id) {
      try {
        // 提示信息
        await this.$confirm('确定要删除该数据吗')
        // 调删除接口
        await delPermission(id)
        // 重新获取数据
        this.getPermissionList()
        // 提示成功信息
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑前获取详情
    async editPermission(id) {
      // 根据获取id获取详情
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 弹层确定按钮
    btnOK() {
      // 第一种写法:
      this.$refs.perForm.validate(async isOK => {
        if (isOK) {
          // 判断有没有id
          if (this.formData.id) {
            // 编辑
            // 2.表单验证通过 发送请求存储数据(后台更新 )
            await updatePermission(this.formData)
          } else {
            // 新增
            // 2.表单验证通过 发送请求存储数据(后台更新 )
            await addPermission(this.formData)
          }
          // 试图更新
          this.getPermissionList()
          this.$message.success(`${this.title}成功`)
          // 关闭弹层
          this.showDialog = false
        }
      })
      // 第二种写法
      this.$refs.perForm.validate().then(() => {
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        return addPermission(this.formData)
      }).then(() => {
        //  提示消息
        this.$message.success(`${this.title}成功`)
        this.getPermissionList()
        this.showDialog = false
      })
    },
    // 取消弹层
    btnCancel() {
      this.showDialog = false
      // 清空表单
      setTimeout(() => {
        this.formData = {
          name: '',
          code: '',
          description: '',
          type: '',
          pid: '',
          enVisible: '0'
        }
      }, 300)
      // 清除校验
      this.$refs.perForm.resetFields()
    }

  }
}
</script>

  <style>

  </style>

